package cn.kgc.javaweb.servlet;

import cn.kgc.javaweb.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author: mosin
 * @version: v1.0  2022/6/22
 *
 * form 表单      method  get  post
 *
 * 处理和用户相关业务  登录  注册  删除 修改
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {

    // 登录
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 获取 用户名 和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 设置响应的编码格式
        resp.setCharacterEncoding("utf-8");
        //设置响应格式
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        User user = new User();
        user.setPassword("1234546");
        user.setUsername("tom");

        /**
         * 页面跳转的方式
         * 1.转发  一次请求    服务器的行为   可以共享数据   req
         * 2.重定向  二次请求   浏览器行为    不能使用请求对象共享数据
         *
         */
        if("admin".equals(username)&&"123456".equals(password)){  // 登录成功  跳转主页

            //resp.sendRedirect(req.getContextPath()+"/index.html");

            writer.write("<table width=\"200px\" height=\"100px\">");
            writer.write("<tr><th>用户名</th><th>密码</th></tr>");
            writer.write("<tr>");
            writer.write("<td>"+user.getUsername()+"</td>");
            writer.write("<td>"+user.getPassword()+"</td>");
            writer.write("</tr>");

            writer.close();

        }else { //登录失败  停留在登录页

            //req.getRequestDispatcher("login.html").forward(req, resp);

            writer.write("用户名或密码不正确!");
            writer.close();

        }

    }

    // 注册
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {



    }





}
